package com.df.barcodescaning.util;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LoggerUtil {

	public static final String APP_ID = "条码扫描";
    private static String logDir = "/Android/ScanBarcode";
    private static String logFileName = "/DF-log.txt";
    private static boolean writeLogsToFile = false;
    private static final int LOG_LEVEL_VERBOSE = 4;
    private static final int LOG_LEVEL_DEBUG = 3;
    private static final int LOG_LEVEL_INFO = 2;
    private static final int LOG_LEVEL_ERROR = 1;
    private static final int LOG_LEVEL_OFF = 0;
    private static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG;

    public static void setLogFileName(String filePrefix) {
        logFileName = filePrefix;
    }

    public static void log(String tag,String message, int logLevel) {
        if (logLevel <= CURRENT_LOG_LEVEL) 
        	if(logLevel != LOG_LEVEL_ERROR){
        		 Log.d(APP_ID, message);
        	}
            if (writeLogsToFile) {
                writeToFile(message);
            }
    }

    public static void writeToFile(String message) {
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + logDir);
            if(!dir.exists())
                dir.mkdirs();
            File file = new File(dir, logFileName);
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024));
            writer.println(APP_ID + " " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " : " + message);
            writer.flush();
            writer.close();
        } catch (Exception e) {
        	Log.e(APP_ID, "Exception in logging  :", e);
        }
    }

    /*static PrintWriter  writer;
    public static void writeToDownloadDir(String message) {
        try {
            if (writer == null){
                File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                File dir = new File(sdCard.getAbsolutePath());
                if(!dir.exists())
                    dir.mkdirs();
                File logFile = new File(sdCard.getPath()+"/"+logFileName);
                boolean isWriteHead = !logFile.exists();
                File f = new File(dir, logFileName);
                writer = new PrintWriter(new BufferedWriter(new FileWriter(f, true), 8 * 1024));
                if (isWriteHead) writer.println("时间,预设值,扫描值,扫描结果");
            }
            String scanTime = new SimpleDateFormat("yyyy年MM月dd HH点mm分ss秒").format(new Date());
            writer.println(scanTime + "," + message);
            writer.flush();
        } catch (Exception e) {
            Log.e(APP_ID, "Exception in logging  :", e);
        }
    }*/

    public static void writeToDownloadDir(String message) {
        try {
            File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            File dir = new File(sdCard.getAbsolutePath());
            if(!dir.exists())
                dir.mkdirs();
            File logFile = new File(sdCard.getPath()+"/"+logFileName);
            boolean isWriteHead = !logFile.exists();
            File f = new File(dir, logFileName);

            byte[] bom ={(byte) 0xEF,(byte) 0xBB,(byte) 0xBF};
            FileOutputStream fos = new FileOutputStream(f, true);
            if (isWriteHead){
                fos.write(bom);
                String title = "时间,预设值,扫描值,扫描结果";
//                    fos.write((new String(title.getBytes(), "GBK")).getBytes());
                fos.write((new String(title.getBytes(), "utf-8")).getBytes());
                fos.write("\n".getBytes());
            }
            String scanTime = new SimpleDateFormat("yyyy年MM月dd HH点mm分ss秒").format(new Date());
            String appStr = scanTime + "," + message;
            fos.write((new String(appStr.getBytes(), "utf-8")).getBytes());
            fos.write("\n".getBytes());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            Log.e(APP_ID, "Exception in logging  :", e);
        }
    }

    public static void verbose(String tag, String message) {
        log(tag, message, LOG_LEVEL_VERBOSE);
    }

    public static void debug(String tag,String message) {
        log(tag, message, LOG_LEVEL_DEBUG);
    }

    public static void error(String tag,String message,Exception ex) {
    	Log.e(tag, message, ex);
        log(tag,message, LOG_LEVEL_ERROR);
    }
    
    public static void error(String tag,String message) {
        log(tag,message, LOG_LEVEL_ERROR);
        Log.e(tag, message);
    }

    public static void info(String tag,String message) {
        log(tag, message, LOG_LEVEL_INFO);
    }
}